package cn.jh.common.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhugu
 * @version 1.0
 * 2018/12/11 16:00
 * Java object conversion map
 */
public class ObjToMap {
    public static Map<String,Object> ConvertObjToMap(Object obj) {
        Map<String,Object> reMap = new HashMap<>();
        if (obj == null) {
            return null;
        }
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                try {
                    Field f = obj.getClass().getDeclaredField(field.getName());
                    f.setAccessible(true);
                    Object o = f.get(obj);
                    reMap.put(field.getName(), o);
                } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return reMap;
    }
}
